using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using WindowsPhoneEssentials.Abstractions;

namespace WindowsPhoneEssentials.Testing.Abstractions
{
    public class FakePhoneApplicationFrame : IPhoneApplicationFrame
    {
        public FakePhoneApplicationFrame()
        {
            CanGoBack = true;
            CanGoForward = true;
        }

        public PageOrientation Orientation { get; set; }

        public Uri Source { get; set; }

        public JournalOwnership JournalOwnership { get; set; }

        public bool CanGoBack { get; set; }

        public bool CanGoForward { get; set; }

        public Uri CurrentSource { get; set; }

        public UriMapperBase UriMapper { get; set; }

        public void StopLoading()
        {

        }

        public void GoBack()
        {

        }

        public void GoForward()
        {

        }

        public event NavigatedEventHandler Navigated;
        public event NavigatingCancelEventHandler Navigating;
        public event NavigationFailedEventHandler NavigationFailed;
        public event NavigationStoppedEventHandler NavigationStopped;
        public event FragmentNavigationEventHandler FragmentNavigation;
        public event EventHandler<OrientationChangedEventArgs> OrientationChanged;
        public event EventHandler<ObscuredEventArgs> Obscured;
        public event EventHandler Unobscured;

        public UIElement GetElement()
        {
            //Likely we are running in a unit test project, return the current root visual
            return Application.Current != null ? Application.Current.RootVisual : null;
        }

        public void OnNavigated(NavigationEventArgs e)
        {
            var handler = Navigated;
            if (handler != null) handler(this, e);
        }

        public void OnNavigating(NavigatingCancelEventArgs e)
        {
            var handler = Navigating;
            if (handler != null) handler(this, e);
        }

        public void OnNavigationFailed(NavigationFailedEventArgs e)
        {
            var handler = NavigationFailed;
            if (handler != null) handler(this, e);
        }


        public void OnNavigationStopped(NavigationEventArgs e)
        {
            var handler = NavigationStopped;
            if (handler != null) handler(this, e);
        }

        public void OnFragmentNavigation(FragmentNavigationEventArgs e)
        {
            var handler = FragmentNavigation;
            if (handler != null) handler(this, e);
        }

        public void OnOrientationChanged(OrientationChangedEventArgs e)
        {
            var handler = OrientationChanged;
            if (handler != null) handler(this, e);
        }

        public void OnObscured(ObscuredEventArgs e)
        {
            var handler = Obscured;
            if (handler != null) handler(this, e);
        }

        public void OnUnobscured(EventArgs e)
        {
            var handler = Unobscured;
            if (handler != null) handler(this, e);
        }

        public bool Navigate(Uri source)
        {
            return true;
        }

#if WINDOWS_PHONE71
        public event EventHandler<JournalEntryRemovedEventArgs> JournalEntryRemoved;

        public void OnJournalEntryRemoved(JournalEntryRemovedEventArgs e)
        {
            var handler = JournalEntryRemoved;
            if (handler != null) handler(this, e);
        }

        public IEnumerable<JournalEntry> BackStack { get; set; }

        public JournalEntry RemoveBackEntry()
        {
            if(BackStack != null)
            {
                var list = BackStack.ToList();
                var last = list.Last();
                list.Remove(last);
                BackStack = list;
                return last;
            }
            return null;
        }
#endif
    }
}